<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!-- 搜索 -->
      <el-input v-model="query.value" clearable placeholder="输入搜索内容" style="width: 200px;" class="filter-item" @keyup.enter.native="toQuery" />
      <el-select v-model="query.type" clearable placeholder="类型" class="filter-item" style="width: 130px">
        <el-option v-for="item in queryTypeOptions" :key="item.key" :label="item.display_name" :value="item.key" />
      </el-select>
      <el-select v-model="query.level" clearable placeholder="会员类型" class="filter-item" style="width: 130px">
        <el-option label="会员" value="99"/>
        <el-option label="游客" value="100"/>
      </el-select>
      <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" @click="toQuery">搜索</el-button>
      <!-- 新增 -->
      <el-button
        type="danger"
        class="filter-item"
        size="mini"
        icon="el-icon-refresh"
        @click="toQuery"
      >刷新</el-button>
    </div>
    <!--表单组件-->
    <eForm ref="form" :is-add="isAdd" />
    <pForm ref="formp" :is-add="isAdd" />
    <detail ref="formd" />
    <!--表格渲染-->
    <el-table v-loading="loading" :data="data" size="small" style="width: 100%;">
      <el-table-column prop="uid" label="用户id" />
      <el-table-column prop="nickname" label="用户昵称" width="180" >
        <template slot-scope="scope">
          <span>{{scope.row.nickname}}</span>
          <!-- {{scope.row.level ? scope.row.isExpire ?'已过期'::''}} -->
          <div v-if="scope.row.level">
            <div v-if="scope.row.isExpire">
             <el-tag size="mini" type="danger">已过期</el-tag>
            </div>
            <div v-else>
              <div v-if="scope.row.expireDays>=0 && scope.row.expireDays<=5">
               <el-tag size="mini" type="danger">剩余{{scope.row.expireDays}}天</el-tag>
              </div>
            </div>
          </div>
   
        </template>
      </el-table-column>
      <el-table-column prop="storeName" width="180" label="所属店铺" />
      <el-table-column ref="table" prop="avatar" label="用户头像">
        <template slot-scope="scope">
          <a :href="scope.row.avatar" style="color: #42b983" target="_blank"><img :src="scope.row.avatar" alt="点击打开" class="el-avatar"></a>
        </template>
      </el-table-column>
      <el-table-column prop="phone" label="手机号码" width="180" />
      <!-- <el-table-column prop="nowMoney" label="用户余额" /> -->
      <!-- <el-table-column prop="brokeragePrice" label="佣金金额" /> -->
      <el-table-column prop="integral" label="用户积分" />
      <el-table-column  prop="createTime" label="创建日期" width="140">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="用户状态" align="center">
        <template slot-scope="scope">
          <div @click="onStatus(scope.row.uid,scope.row.status)">
            <el-tag v-if="scope.row.status == 1" style="cursor: pointer" :type="''">正常</el-tag>
            <el-tag v-else style="cursor: pointer" :type=" 'info' ">禁用</el-tag>
          </div>
        </template>
      </el-table-column> -->

      <el-table-column label="会员状态" align="center">
        <template slot-scope="scope">
          {{scope.row.level ?scope.row.isExpire?'已过期':'正常':''}}
        </template>

      </el-table-column>
      <!-- <el-table-column label="用户来源" align="center">
        <template slot-scope="scope">
          <div>
            <el-tag v-if="scope.row.userType == 'wechat'">公众号</el-tag>
            <el-tag v-else-if="scope.row.userType == 'routine'">小程序</el-tag>
            <el-tag v-else>H5</el-tag>
          </div>
        </template>
      </el-table-column> -->
      <el-table-column prop="spreadUid" label="会员套餐" width="130">
        <template slot-scope="scope">
          {{scope.row.level ? scope.row.yxSystemUserLevelQueryVo.levelName + scope.row.yxSystemUserLevelQueryVo.name:'游客'}}
        </template>
      </el-table-column>
      <el-table-column v-if="checkPermission(['admin','YXUSER_ALL','YXUSER_EDIT','YXUSER_DELETE'])" label="操作" width="215" align="center" fixed="right">
        <template slot-scope="scope">
          <!-- <el-button
            v-permission="['admin','YXUSER_ALL','YXUSER_EDIT']"
            size="mini"
            type="danger"
            @click="editD(scope.row)"
          >查看下级</el-button> -->
          <el-dropdown size="mini" split-button type="primary" trigger="click">
            操作
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>
                <el-button
                  v-permission="['admin','YXUSER_ALL','YXUSER_EDIT']"
                  size="mini"
                  type="primary"
                  @click="edit(scope.row)"
                >修改用户</el-button>
              </el-dropdown-item>
              <!-- <el-dropdown-item>
                <el-button
                  v-permission="['admin','YXUSER_ALL','YXUSER_EDIT']"
                  size="mini"
                  type="primary"
                  @click="editP(scope.row)"
                >修改余额</el-button>
              </el-dropdown-item> -->
              <el-dropdown-item>
                <el-button
                  v-permission="['admin','YXUSER_ALL','YXUSER_EDIT']"
                  size="mini"
                  type="primary"
                  @click="editVip(scope.row)"
                >修改会员</el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button
                  v-permission="['admin','YXUSER_ALL','YXUSER_EDIT']"
                  size="mini"
                  type="primary"
                  @click="editTime(scope.row)"
                >修改时间</el-button>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <el-pagination
      :total="total"
      :current-page="page + 1"
      style="margin-top: 8px;"
      layout="total, prev, pager, next, sizes"
      @size-change="sizeChange"
      @current-change="pageChange"
    />

    <el-dialog :append-to-body="true" :close-on-click-modal="false" :before-close="cancelVip" :visible.sync="vipVis" title="会员" width="500px">
    <el-form ref="vipForm" :model="vipForm" :rules="vipRules" size="small" label-width="80px">
      <el-form-item label="会员" prop="levelId">
        <el-select style="width:100%"  v-model="vipForm.levelId" placeholder="请选择">
          <el-option
            v-for="item in vipList"
            :key="item.id"
            :label="item.levelName + item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancelVip">取消</el-button>
      <el-button :loading="loading" type="primary" @click="submitVip">确认</el-button>
    </div>
  </el-dialog>


  <el-dialog :append-to-body="true" :close-on-click-modal="false" :before-close="cancelTime" :visible.sync="timeVis" title="修改时间" width="400px">
    <el-form ref="timeForm" :model="timeForm"  size="small" label-width="80px">
      <el-form-item label="天数" prop="day">
        <el-input-number style="width:80%" v-model="timeForm.day"  ></el-input-number>
      </el-form-item>
      
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancelTime">取消</el-button>
      <el-button :loading="loading" type="primary" @click="submitTime">确认</el-button>
    </div>
  </el-dialog>
  </div>
</template>

<script>
import checkPermission from '@/utils/permission'
import initData from '@/mixins/crud'
import { del, onStatus,setVipTime } from '@/api/yxUser'
import eForm from './form'
import pForm from './formp'
import detail from './detail'
import { formatTime } from '@/utils/index'
import { getVipList,addVip } from '@/api/yxSystemUserLevel'
export default {
  components: { eForm, pForm, detail },
  mixins: [initData],
  data() {
    return {
      delLoading: false,
      userType: '',
      queryTypeOptions: [
        { key: 'nickname', display_name: '用户昵称' },
        { key: 'phone', display_name: '手机号码' },
      ],
      statusOptions: [
        { value: 'routine', label: '小程序' },
        { value: 'wechat', label: '公众号' },
        { value: 'H5', label: 'H5' }
      ],
      vipVis:false,
      vipForm:{
        uid:null,
        levelId:null
      },
      vipList:[],
      vipRules:{
        levelId: [
            { required: true, message: '请选择会员', trigger: 'change' }
          ],
      },
      timeVis:false,
      timeForm:{
        uid:null,
        day:0
      }
    }
  },
  created() {
    this.$nextTick(() => {
      this.init()
    })
    this.getVip()
  },
  methods: {
    formatTime,
    checkPermission,
    submitTime(){
      this.loading = true
      setVipTime(this.timeForm).then((res)=>{
        console.log("🚀 ~ setVipTime ~ res:", res)
        this.loading = false
        this.timeVis = false
        this.$message.success('修改成功')
        this.$nextTick(() => {
          this.init()
        })
      }).catch(()=>{
        this.loading = false
      })
    },
    cancelTime(){
      this.timeVis = false
    },
    submitVip(){
      this.$refs['vipForm'].validate((valid) => {
        addVip(this.vipForm).then(res=>{
          this.$message.success('操作成功')
          this.cancelVip()
          this.init()
        })
      });
     
    },
    cancelVip(){
      this.vipVis = false
      this.$refs['vipForm'].resetFields();
    },
    getVip(){
      getVipList().then(res=>{
        this.vipList = res.content
      })
    },
    editVip(row){
      this.vipVis = true
      this.vipForm.uid = row.uid
      this.vipForm.levelId = row.level
    },

    editTime(row){
      this.timeVis = true
      this.timeForm.uid = row.uid
      this.timeForm.day = 0
    },

    
    onStatus(id, status) {
      this.$confirm(`确定进行[${status ? '禁用' : '开启'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          onStatus(id, { status: status }).then(({ data }) => {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1000,
              onClose: () => {
                this.init()
              }
            })
          })
        })
        .catch(() => { })
    },
    beforeInit() {
      this.url = 'api/yxUser'
      const sort = 'uid,desc'
      this.params = { page: this.page, size: this.size, sort: sort, userType: this.userType }
      const query = this.query
      const type = query.type
      const value = query.value
      if (type && value) { this.params[type] = value }
      return true
    },
    subDelete(uid) {
      this.delLoading = true
      del(uid).then(res => {
        this.delLoading = false
        this.$refs[uid].doClose()
        this.dleChangePage()
        this.init()
        this.$notify({
          title: '删除成功',
          type: 'success',
          duration: 2500
        })
      }).catch(err => {
        this.delLoading = false
        this.$refs[uid].doClose()
        console.log(err.response.data.message)
      })
    },
    add() {
      this.isAdd = true
      this.$refs.form.dialog = true
    },
    edit(data) {
      this.isAdd = false
      const _this = this.$refs.form
      _this.form = {
        uid: data.uid,
        account: data.account,
        pwd: data.pwd,
        realName: data.realName,
        birthday: data.birthday,
        cardId: data.cardId,
        mark: data.mark,
        partnerId: data.partnerId,
        groupId: data.groupId,
        nickname: data.nickname,
        avatar: data.avatar,
        phone: data.phone,
        addTime: data.addTime,
        addIp: data.addIp,
        lastTime: data.lastTime,
        lastIp: data.lastIp,
        nowMoney: data.nowMoney,
        brokeragePrice: data.brokeragePrice,
        integral: data.integral,
        signNum: data.signNum,
        status: data.status,
        level: data.level,
        spreadUid: data.spreadUid,
        spreadTime: data.spreadTime,
        userType: data.userType,
        isPromoter: data.isPromoter,
        payCount: data.payCount,
        spreadCount: data.spreadCount,
        cleanTime: data.cleanTime,
        addres: data.addres,
        adminid: data.adminid,
        loginType: data.loginType
      }
      _this.dialog = true
    },
    editP(data) {
      this.isAdd = false
      const _this = this.$refs.formp
      _this.form = {
        uid: data.uid,
        nickname: data.nickname,
        ptype: 1,
        money: 0
      }
      _this.dialog = true
    },
    editD(data) {
      const _this = this.$refs.formd
      _this.form = {
        uid: data.uid,
        nickname: data.nickname
      }
      _this.dialog = true
      _this.spread(data.uid)
    }
  }
}
</script>

<style scoped>

</style>
